package com.orgcent.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;

import com.orgcent.activities.SMSSender;
import com.orgcent.util.Tools;

/**
 * 发送短信时，接收短信发送后的状态
 */
public class SmsStatusReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();

		Log.i("sms", getResultData() + ",resultcode=" + getResultCode());

		Bundle b = getResultExtras(true);
		for (String key : b.keySet()) {
			Log.i("sms", "name=" + key + ",value=" + b.get(key));
		}

		if (SMSSender.ACTION_SMS_SENT.equals(action)) {
			processSentResult(context);
		} else if (SMSSender.ACTION_SMS_DELIVERY.equals(action)) {
			processDeliveryResult(context);
		}
	}

	private void processSentResult(Context context) {
		String msg;
		switch (getResultCode()) {
		case Activity.RESULT_OK:
			msg = "短信发送成功";
			break;
		case SmsManager.RESULT_ERROR_NO_SERVICE:
			msg = "服务不可用";
			break;
		case SmsManager.RESULT_ERROR_RADIO_OFF:
			msg = "手机信号被关闭";
			break;
		case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
		case SmsManager.RESULT_ERROR_NULL_PDU:
		default:
			msg = "发送失败";
			break;
		}
		Tools.toastShow(context, msg);
	}

	private void processDeliveryResult(Context context) {
		String msg;
		switch (getResultCode()) {
		case Activity.RESULT_OK:
			msg = "对方已收到短信";
			break;
		case Activity.RESULT_CANCELED:
		default:
			msg = "对方未收到短信";
			break;
		}
		Tools.toastShow(context, msg);
	}
}
